/**
 * Created by Administrator on 2017/6/27.
 */
var Promise = require('bluebird');
var Comment = Promise.promisifyAll(require('../models/comment'));

//comment进行评论
exports.save = function *(req, res) {
  var comment = req.body;
  comment.time = Date.now();
  if (comment.commentId) {  //进行回复
    var _oldComment = yield Comment.findOne({_id: comment.commentId});
    _oldComment.replies.push(comment);
    var doc = yield Comment.update({_id: comment.commentId}, _oldComment);
    if (doc.nModified == 1) {
      res.redirect('/movie/detail/' + comment.movie);
    }
  } else {  //新的评论
    var newComment = new Comment(comment);
    var _comment = yield newComment.save();
    if (_comment) {
      res.redirect('/movie/detail/' + comment.movie);
    }
  }
}